<?php
/**
 * Controller is the customized base controller class.
 * All controller classes for this application should extend from this base class.
 */
class Controller extends CController
{
	/**
	 * @var string the default layout for the controller view. Defaults to '//layouts/column1',
	 * meaning using a single column layout. See 'protected/views/layouts/column1.php'.
	 */
	public $layout='//layouts/column1';
	/**
	 * @var array context menu items. This property will be assigned to {@link CMenu::items}.
	 */
	public $menu=array();
	/**
	 * @var array the breadcrumbs of the current page. The value of this property will
	 * be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links}
	 * for more details on how to specify this property.
	 */
	public $breadcrumbs=array();
        
        /*
         * On every Controller instance set language from get if is index page get main language
         */
        public function init(){
                if(isset($_GET['lang']))
                    Yii::app()->setLanguage($_GET['lang']);
                else
                    Yii::app()->setLanguage('vi');
                parent::init();
        }
// 
//       /*
//         * ProcesUrl method used for resolving languages if dont have get language
//         * System gets default language defined from database
//         */
        public static function processUrl(){
                if(isset($_GET['lang']))
                    Yii::app()->setLanguage($_GET['lang']);
                else
                    Yii::app()->setLanguage('vi');
        }
 
         /*
         * Aftereach action processUrl
         */
         protected function beforeAction($action) {
            self::processUrl();
            return parent::beforeAction($action);
        }
        
        protected function getPaginationProductListByCriteria(
                $criteria, 
                $pageSize)
        {
            // $product and $pageSize must be set.
            if (!isset($criteria) || !isset($pageSize))
                return;

            // Count total number of products.
            $count = SanPham::model()->count($criteria);
            
            // Pagination.
            $pages = new CPagination($count);
            $pages->pageSize = $pageSize;
            $pages->applyLimit($criteria);
            
            // Get products to show with criteria.
            $models = SanPham::model()->findAll($criteria);

            return array('models' => $models,
                         'pages' => $pages);
        }
}